import 'dart:ui';

import 'package:chat_app_project/models/const.dart';
import 'package:chat_app_project/models/user_login_info_entity.dart';
import 'package:chat_app_project/screens/messages/components/text_message.dart';
import 'package:flutter/material.dart';
import 'package:wukongimfluttersdk/type/const.dart';

import '../../../const/sys_const.dart';
import '../../../constants.dart';
import '../../../models/ChatMessage.dart';
import '../../../models/msg.dart';
import '../../../util/sp_util.dart';
import 'audio_message.dart';
import 'convention_message.dart';
import 'image_message.dart';
import 'video_message.dart';

class Message extends StatelessWidget {
  const Message({
    Key? key,
    required this.message,
  }) : super(key: key);

  final UIMsg message;

  @override
  Widget build(BuildContext context) {
    //根据类型选择控件
    Widget messageContaint(UIMsg message) {
      switch (message.wkMsg.contentType) {
        case WkMessageContentType.text:
          return TextMessage(message: message);
        case WkMessageContentType.voice:
          return AudioMessage(message: message);
        case WkMessageContentType.video:
          return VideoMessage(message: message);
        case WkMessageContentType.image:
          return ImageMessage(message: message);
        case 2001:
          return ConventionMessage(message: message);
        default:
          return const SizedBox();
      }
    }

    //获取登录信息 --start
    UserLoginInfoEntity loginInfoEntity = UserLoginInfoEntity();
    Map<String, dynamic>? loginInfo = SpUtil.getObject("loginInfo");
    if (loginInfo != null) {
      loginInfoEntity = UserLoginInfoEntity.fromJson(loginInfo);
    }
    //获取登录信息 --end
    //是否自己发送
    var isSender = message.wkMsg.fromUID == loginInfoEntity.id.toString();

    return Padding(
      padding: const EdgeInsets.only(top: kDefaultPadding),
      child: Row(
        mainAxisAlignment:
            isSender ? MainAxisAlignment.end : MainAxisAlignment.start,
        children: [
          if (!isSender) ...[
            CircleAvatar(
              radius: 20,
              backgroundImage: message.wkMsg.getChannelInfo() == null
                  ? const NetworkImage(SysConst.defaultAvatar)
                  : NetworkImage(message.wkMsg.getChannelInfo()!.avatar),
            ),
            const SizedBox(
              width: kDefaultPadding / 2,
            )
          ],
          //发送状态
          if (isSender)
            Image(
                image: AssetImage(message.getStatusIV()),
                width: 30,
                height: 30),
          const SizedBox(
            width: kDefaultPadding / 10,
          ),
          //Flexible控制溢出
          Flexible(child: messageContaint(message)),
          if (isSender) ...[
            const SizedBox(
              width: kDefaultPadding / 2,
            ),
            CircleAvatar(
              radius: 20,
              backgroundImage: NetworkImage(
                  loginInfoEntity.avatar ?? SysConst.defaultAvatar),
            ),
          ]
        ],
      ),
    );
  }

}

